
#include "bkwl_common.h"
#include "bkwl_mqtt.h"
#include "cJSON.h"
#include "bkwl_bk_doorbell.h"
#include "bkwl_doorbell_main.h"

bk_err_t bkwl_mqtt_sibo_heartbeat_send()
{
    bk_err_t ret = BK_OK;

    // tprintf("twh bkwl_mqtt_sibo_heartbeat_send entry\n");
    // char *p_send_buf = "{\"ver\":\"1.0.0\",\"act\":\"0002\",\"seq_no\":\"123456567786754655464\"}";
    // char *p_send_buf = "aaa";
    char *topic_send = bkwl_main->topic.MQTT_PROPERTY_EVENT_POST;
    cJSON *root = NULL;
    char *out = NULL;
    root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "act", BKWL_SIBO_MQTT_PROTOCOL_DEVICE_HEARTBEAT_SEND); // 设备心跳信息
    cJSON_AddStringToObject(root, "ver", BKWL_IOT_VER_CUR);
    cJSON_AddStringToObject(root, "seq_no", "123456567786754655464");
    out = cJSON_Print(root);
    bkwl_mqtt_cmd_msg_send(topic_send, out);
    cJSON_Delete(root);
    free(out);
    //  tprintf("twh bkwl_mqtt_sibo_heartbeat_send exit\n");
    return ret;
}

// 属性上报：
// oTopic：/sys/${product_key}/${device_name}/event/property/post
// oPayload：
// 1.{"ver":"1.0.0","act":"0003","type":"01","data":"0.95","seq_no":"xxxxxxxxxx"}    
bk_err_t bkwl_mqtt_sibo_property_send(char *type, char *data)
{
    bk_err_t ret = BK_OK;

    //  tprintf("twh bkwl_mqtt_sibo_property_send entry\n");
    // char *p_send_buf = "{\"ver\":\"1.0.0\",\"act\":\"0002\",\"seq_no\":\"123456567786754655464\"}";
    // char *p_send_buf = "aaa";
    char *topic_send = bkwl_main->topic.MQTT_PROPERTY_EVENT_POST;
    cJSON *root = NULL;
    char *out = NULL;
    root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "act", BKWL_SIBO_MQTT_PROTOCOL_DEVICE_HEARTBEAT_SEND); // 设备心跳信息
    cJSON_AddStringToObject(root, "ver", BKWL_IOT_VER_CUR);
    cJSON_AddStringToObject(root, "type", type);
    cJSON_AddStringToObject(root, "data", data);
    cJSON_AddStringToObject(root, "seq_no", "123456567786754655464");
    out = cJSON_Print(root);
    bkwl_mqtt_cmd_msg_send(topic_send, out);
    cJSON_Delete(root);
    free(out);
    //  tprintf("twh bkwl_mqtt_sibo_property_send exit\n");
    return ret;
}

// 事件上报：
// oTopic：/sys/${product_key}/${device_name}/event/notify/post
// oPayload：
// {"ver":"1.0.0","act":"0004","type":"01","data":"","user":"01","utype":"1","seq_no":"xxxxxxxxxx"} 
bk_err_t bkwl_mqtt_sibo_notify_send(char *type, char *user, char *data, char *utype)
{
    bk_err_t ret = BK_OK;

    // tprintf("twh bkwl_mqtt_sibo_notify_send entry\n");
    char *topic_send = bkwl_main->topic.MQTT_NOTIFY_EVENT_POST;
    cJSON *root = NULL;
    char *out = NULL;
    root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "act", BKWL_SIBO_MQTT_PROTOCOL_DEVICE_NOTIFY_SEND); // 设备事件上报
    cJSON_AddStringToObject(root, "ver", BKWL_IOT_VER_CUR);
    cJSON_AddStringToObject(root, "type", type);
    cJSON_AddStringToObject(root, "user", user);
    cJSON_AddStringToObject(root, "utype", utype);
    cJSON_AddStringToObject(root, "data", data);
    cJSON_AddStringToObject(root, "seq_no", "123456567786754655464");
    out = cJSON_Print(root);
    bkwl_mqtt_cmd_msg_send(topic_send, out);
    cJSON_Delete(root);
    free(out);
    //  tprintf("twh bkwl_mqtt_sibo_notify_send exit\n");
    return ret;
}

// // 事件上报响应：
// // oTopic：/sys/${product_key}/${device_name}/event/notify/post_reply
// // oPayload：
// // {"ver":"1.0.0","act":"0005","sta":"00","seq_no":"xxxxxxxxxx"} 
// bk_err_t bkwl_mqtt_sibo_notify_reply_send(char *name, char *data)
// {
//     bk_err_t ret = BK_OK;

//     // tprintf("twh bkwl_mqtt_sibo_notify_reply_send entry\n");
//     // char *p_send_buf = "{\"ver\":\"1.0.0\",\"act\":\"0002\",\"seq_no\":\"123456567786754655464\"}";
//     // char *p_send_buf = "aaa";
//     char *topic_send = bkwl_main->topic.MQTT_NOTIFY_EVENT_POST_REPLY;
//     cJSON *root = NULL;
//     char *out = NULL;
//     root = cJSON_CreateObject();
//     cJSON_AddStringToObject(root, "act", BKWL_SIBO_MQTT_PROTOCOL_DEVICE_NOTIFY_REPLY); // 设备事件上报响应
//     cJSON_AddStringToObject(root, "ver", BKWL_IOT_VER_CUR);
//     cJSON_AddStringToObject(root, name, data);
//     cJSON_AddStringToObject(root, "seq_no", "123456567786754655464");
//     out = cJSON_Print(root);
//     bkwl_mqtt_cmd_msg_send(topic_send, out);
//     cJSON_Delete(root);
//     free(out);
//     // tprintf("twh bkwl_mqtt_sibo_notify_reply_send exit\n");
//     return ret;
// }

// 配网成功模块上报数据格式
// oTopic：/sys/${product_key}/${device_name}/event/property/post
// oPayload： 

bk_err_t bkwl_mqtt_sibo_pair_net(char *token)
{
    bk_err_t ret = BK_OK;
    char *topic_send = bkwl_main->topic.MQTT_PROPERTY_EVENT_POST;
    cJSON *root = NULL;
    char *out = NULL;
    root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "act", BKWL_SIBO_MQTT_PROTOCOL_DEVICE_PAIR_NET); // 设备事件上报响应
    cJSON_AddStringToObject(root, "ver", BKWL_IOT_VER_CUR);
    cJSON_AddStringToObject(root, "sta", "00");
    cJSON_AddStringToObject(root, "token", token);
    cJSON_AddStringToObject(root, "seq_no", "123456567786754655464");
    out = cJSON_Print(root);
    bkwl_mqtt_cmd_msg_send(topic_send, out);
    cJSON_Delete(root);
    free(out);
    return ret;
}

bk_err_t bkwl_mqtt_sibo_upload_image()
{
    bk_err_t ret = BK_OK;
    char *type = "03";
    char *user = "01";
    char *utype = "1";
    char *data = "";
    ret = bkwl_mqtt_sibo_notify_send(type, user, data, utype);
    return ret;
}
